package iks.utils.ui 
{
	import flash.display.*;
	import flash.events.*;
	
	public final class KInput 
	{
		/// 设置是否允许其他显示对象获得键盘焦点
		public static var allowFocus : Boolean = false;
		
		/// 初始化输入, 在KGame中已经初始化, 不需要再调用
		public static function initInput(stage : Stage) : void
		{
			// 初始化按键
			if (m_keyStates == null)
			{
				m_keyStates = new Vector.<KeyState>(KEY_SIZE, true);
				for (var k : int = 0; k < KEY_SIZE; ++k)
					m_keyStates[k] = new KeyState();
					
				// 初始化鼠标
				m_mouseStatus = new MouseStatus();
				
				// 添加侦听器
				m_stage = stage;
				m_stage.focus = stage;
				m_stage.doubleClickEnabled = true;
				m_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
				m_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
				m_stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
				m_stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
				m_stage.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
			}
		}
		
		/// 按键更新
		public static function update() : void
		{
			for (var i : int = 0; i < KEY_SIZE; ++i)
			{
				var keyState : KeyState = m_keyStates[i];
				if (keyState.status != 0)
					++keyState.status;
			}
			
			// 鼠标的逻辑与键盘的相同
			if (m_mouseStatus.lStatus != 0)
				++m_mouseStatus.lStatus;
				
			m_mouseStatus.doubleClick = false;
		}
		
		/// 按键是否按下
		public static function isKeyDown(key : int) : Boolean
		{
			return m_keyStates[key].status >= 1
		}
		
		/// 按键是否抬起
		public static function isKeyUp(key : int) : Boolean
		{
			return m_keyStates[key].status == -1;
		}
		
		/// 按键是否单次按下
		public static function isKeyPress(key : int) : Boolean
		{
			return m_keyStates[key].status == 1;
		}
		
		/// 鼠标是否按下状态
		public static function isMouseDown() : Boolean
		{
			return m_mouseStatus.lStatus >= 1;
		}
		
		/// 鼠标是否按下
		public static function isMousePress() : Boolean
		{
			return m_mouseStatus.lStatus == 1;
		}
		
		/// 鼠标是否双击
		public static function isDoubleClick() : Boolean
		{
			return m_mouseStatus.doubleClick;
		}
		
		/// 鼠标是否抬起
		public static function isMouseRelease() : Boolean
		{
			return m_mouseStatus.lStatus == -1;
		}
		
///////////////////////////////////////////////////////////////////////////////////////////////////
// 私有域
///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/// 键盘按下事件
		private static function keyDownHandler(evt : KeyboardEvent) : void
		{
			if (!allowFocus)
				m_stage.focus = m_stage;
			
			++m_keyStates[evt.keyCode].status;
		}
		
		/// 键盘抬起事件
		private static function keyUpHandler(evt : KeyboardEvent) : void
		{
			if (!allowFocus)
				m_stage.focus = m_stage;
			
			m_keyStates[evt.keyCode].status = -1;
		}
		
		/// 鼠标按下事件
		private static function mouseDownHandler(evt : MouseEvent) : void
		{
			if (!allowFocus)
				m_stage.focus = m_stage;
			
			if (evt.type == MouseEvent.MOUSE_DOWN)
				m_mouseStatus.lStatus = 1;
		}
		
		/// 鼠标抬起事件
		private static function mouseUpHandler(evt : MouseEvent) : void
		{
			if (!allowFocus)
				m_stage.focus = m_stage;
			
			if (evt.type == MouseEvent.MOUSE_UP)
				m_mouseStatus.lStatus = -1;
		}

		/// 鼠标双击事件
		private static function doubleClickHandler(evt : MouseEvent) : void
		{
			if (!allowFocus)
				m_stage.focus = m_stage;
			
			m_mouseStatus.doubleClick = true;
		}
		
		private static var m_keyStates		: Vector.<KeyState> = null;
		private static var m_mouseStatus	: MouseStatus = null;
		private static var m_stage			: Stage = null;
		private static const KEY_SIZE		: int = 255;

	}

}



































// ============================================================================
//
// end file
//
// ============================================================================


